package com.franklin.icharm;

import java.util.ArrayList;
import java.util.List;

import com.franklin.icharm.SetUpAppActivity.Attribute;
import com.franklin.icharm.SetUpAppActivity.MyAdapters;
import com.franklin.icharm.SetUpAppActivity.MyAdapters.ListItems;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SetUpSMSActivity extends Activity {
	private static final String TAG = "SetUpSMSActivity";
	private static final int REQ_CODE_SELECT_RECIPIENT = 1;

	private ListView lv;
	private ImageView iv;
	private ImageView save;
	private EditText et;
	MyAdapters adapter;
	private List<Attribute> menu;
	Attribute data[] = new Attribute[] { new Attribute("Select recipient",R.drawable.arrow) };
	private int number;
	private int position;
	private String name;
	private UserConfig uc;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_up_sms);
		lv = (ListView) findViewById(R.id.contact_list);
		iv = (ImageView) findViewById(R.id.exit);
		et = (EditText) findViewById(R.id.et_message);
		save = (ImageView) findViewById(R.id.save);
		uc = new UserConfig(this);
		Intent intent = getIntent();
		number = intent.getExtras().getInt("number");
		position = intent.getExtras().getInt("position");
		name = intent.getExtras().getString("name");
		iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String n_name=uc.getConfString(UserConfig.KV_TOUCH0_SMS_NAME.replace("{0}",
								String.valueOf(number)), null);
				String names;
				if(name==null)
					names=n_name;
				else
					names=name;
				uc.setConf(
						UserConfig.KV_TOUCH0_SMS_NAME.replace("{0}",
								String.valueOf(number)), names);
				uc.setConf(UserConfig.KV_TOUCH0_SMS_MSG.replace("{0}",
						String.valueOf(number)), et.getText().toString());
/*				Intent intent=new Intent();
				intent.putExtra("num", 0);
				intent.setClass(SetUpSMSActivity.this,
						SelectActionActivity.class);
				startActivity(intent);*/
				finish();
				// uc.setConf(UserConfig.KV_TOUCH0_SMS_NAME.replace("{0}",
				// String.valueOf(number)), position);
			}
		});
		try {

			new Thread() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					try {
						menu = parseJson();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					handler.sendEmptyMessage(0);
				}
			}.start();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String message = uc.getConfString(UserConfig.KV_TOUCH0_SMS_MSG.replace("{0}",
				String.valueOf(number)), null);
		if(message!=null && message!="")
			et.setText(message);
	}

	private Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			adapter = new MyAdapters(SetUpSMSActivity.this, menu);
			lv.setAdapter(adapter);
			// lv.setOnItemClickListener(listener);
			lv.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					Intent intent = new Intent();
					if (arg2 == 0) {
						intent.putExtra("number", number);
						intent.setClass(SetUpSMSActivity.this,
								SelectRecipientActivity.class);
						startActivityForResult(intent, REQ_CODE_SELECT_RECIPIENT);
					}
				}
			});
		};
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		switch (requestCode)
		{
			case REQ_CODE_SELECT_RECIPIENT:
				if (resultCode == RESULT_OK)
				{
					number = data.getExtras().getInt("number");
					position = data.getExtras().getInt("position");
					name = data.getExtras().getString("name");		
					Log.i(TAG, "Selected recipient: " + number + " " + name);
				}
				break;
		} // switch
	} // onActivityResult

	public List<Attribute> parseJson() throws Exception {
		List<Attribute> list = new ArrayList<Attribute>();
		for (int i = 0; i < data.length; i++) {
			Attribute obj = new Attribute();
			obj.SetText(data[i].GetText());
			obj.SetName(data[i].GetName());
			list.add(obj);
		}

		return list;
	}

	public class MyAdapters extends BaseAdapter {
		private List<Attribute> currency = null;
		private LayoutInflater linearlayout;

		public MyAdapters(Context con, List<Attribute> listcurrency) {
			linearlayout = LayoutInflater.from(con);
			currency = listcurrency;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return currency.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return currency.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ListItems listitems;
			if (convertView == null) {
				listitems = new ListItems();
				convertView = linearlayout.inflate(
						R.layout.setupsms_items_view, null);
				listitems.name = (ImageView) convertView
						.findViewById(R.id.name_1);
				listitems.text = (TextView) convertView
						.findViewById(R.id.name_2);
				convertView.setTag(listitems);
			} else {
				listitems = (ListItems) convertView.getTag();
			}
			Attribute cur = currency.get(position);
			listitems.name.setImageResource(cur.GetName());
			listitems.text.setText(cur.GetText());
			return convertView;
		}

		class ListItems {
			private TextView text;
			private ImageView name;
		}

	}

	public class Attribute {
		private int Name;
		private String Text;

		public Attribute() {
			super();
		}

		public Attribute(String text, int name) {
			super();
			this.Text = text;
			this.Name = name;
		}

		public void SetName(int _Name) {
			Name = _Name;
		}

		public void SetText(String _text) {
			Text = _text;
		}

		public int GetName() {
			return Name;
		}

		public String GetText() {
			return Text;
		}
	}
}
